/**
 *	@file	debug.h
 *
 *	@brief	the debug functions
 *
 *	@author	forrest.zhang
 *
 *	@date	2009-09-09
 */

#ifndef FZ_DEBUG_H
#define FZ_DEBUG_H

#include <stdio.h>
#include <string.h>
#include <errno.h>

/* the debug level: 
 * 0	didn't show debug information.
 * 1	show flow information.
 * 2	show init information and flow information.
 * 3	show all debug information 
 */
extern int	g_dbglvl;

/* show data flow debug information */
#define FLOW(level, fmt, args...)		\
	if ((level + 1) <= g_dbglvl)			\
		printf(fmt, ##args);   

/* print debug message */
#define DBG(fmt, args...)			\
	if (g_dbglvl)				\
		printf("proxyd: "fmt, ##args)

/* print buffer content */
#define DBGBUF(buf, size, fmt, args...) 	\
	if (g_dbglvl > 2)	{		\
		printf("[buf]: "fmt, ##args);	\
		do {				\
			write(0, buf, size);	\
			fsync(0);		\
			printf("\n\n");		\
		} while (0);			\
	}

/* print error message */
#define ERR(fmt, args...)			\
	fprintf(stderr, "[ERR]:%s:%d: " fmt,	\
		__FILE__, __LINE__, ##args)
#define	ERRSTR		strerror(errno)

/* likely()/unlikely() for performance */
#ifndef unlikely
#define unlikely(x)  __builtin_expect(!!(x), 0)
#endif

#ifndef likely
#define likely(x)    __builtin_expect(!!(x), 1)
#endif


#endif /* end of FZ_DEBUG_H */

